<?php
error_reporting(0);
define('IN_QY',true);
session_start();
require("include/common.inc.php");
define('SCRIPT','ad_list');
require_once('config.php');
$infoid=trim($_GET['fid']);
if($infoid){
setcookie("dzfhaigoufid",$_GET['fid'],time()+(3600*12*1*1),'/');	
$_COOKIE['dzfhaigoufid']=$infoid;	
}


function is_weixin() {
   if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
       return true;
    } return false;
 }
 $is_weixin = (is_weixin() == true) ? "1" : "2";

  if($is_weixin==1){
	  
if($_COOKIE['openid']==""){	  
header("Content-type: text/html; charset=utf-8");
include_once('dependency/WxPayPubHelper/WxPayPubHelper.php');	  
$jsApi = new JsApi_pub();
//网页授权获取用户openid
//通过code获得openid
$openid = "";
try {
    if (!isset($_GET['code'])) {
        //触发微信返回code码
        $url = $jsApi->createOauthUrlForCode("http://www.goodshare.com.cn/haigou/wxdzfhailogin.php");
        Header("Location: $url");
    } else {
        //获取code码，以获取openid
        $code = $_GET['code'];
        $jsApi->setCode($code);
        $openid = $jsApi->getOpenId();
		setcookie("openid",$openid,time()+(3600*8*1*1),"/"); 
    }
} catch (Exception $e) {
 echo $e->getMessage();
    exit();
}

} else{
	
$openid =$_COOKIE['openid'];	
}


  }
  
  
    $sql_user = "select * from tbl_taorder where orderid='".$_COOKIE['dzfhaigoufid']."'";
	$queryuser=mysql_query($sql_user);
	$rowuht=mysql_fetch_array($queryuser);
//
    $sql_user1 = "select * from tbl_taospp where id='".$rowuht['goodid']."'";
	$queryuser1=mysql_query($sql_user1);
	$rowuu=mysql_fetch_array($queryuser1);	

$sql = "select sum(totprice+expresstri+taxpri-yhqmoney) as tot from tbl_taorder where uid='".$_COOKIE['userid']."' and is_pay=3 AND orderid='".$_COOKIE['dzfhaigoufid']."' ";
$query = mysql_query($sql);
$rows = mysql_fetch_array($query);
$temtot = round($rows['tot'], 2);

$sql1 = "select max(id) as maxid,orderid,addtime from tbl_taorder where uid='".$_COOKIE['userid']."' and is_pay=3 ";
$query1 = mysql_query($sql1);
$row1 = mysql_fetch_array($query1);

header("Content-type: text/html; charset=utf-8"); 
$app_id = "b6b437fa-c5cf-4e88-801b-c51696582283";
$app_secret = "1e08c791-1384-424e-81ae-5a31f1333add";
$amount =  (int)($temtot*100);//支付总价
$title = mb_substr($rowuu['chtitle'],0,15,'utf-8');
$out_trade_no = $_COOKIE['dzfhaigoufid'];//订单号，需要保证唯一性
//1.生成sign
$sign = md5($app_id . $title . $amount . $out_trade_no . $app_secret);
//支付吊起结束


?>

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>待支付-众晒网</title>
    <link rel="stylesheet" type="text/css" href="/ui/css/Hui.css" />
    <style type="text/css">
        #header{width:100%; height:44px; position:fixed; top:0;z-index:999;}
		<!----
        #footer{width:100%; height:55px; position:absolute; bottom:0;}
		----->
        #lis{width:100%; overflow: auto; position:absolute; margin-top:44px;bottom:55px;}	
    </style>	
</head>
<body >
    <header class="H-header H-theme-background-color-white" id="header">
        <span tapmode="" onclick="H.openWin('obligations', '/wap/my.php');" class="H-icon H-position-relative H-display-inline-block H-float-left H-vertical-middle H-theme-font-color-gray H-padding-horizontal-left-5 H-z-index-100"><i class="H-iconfont H-icon-arrow-left H-font-size-18 H-vertical-middle"style="color: white;"></i></span>
        <div class="H-header-title H-center-all H-font-size-18 H-text-show-row-1 H-theme-font-color-black H-position-absolute H-width-100-percent" style="color: white; background: linear-gradient(to right, #3982e6 , #5cccfb); /* 标准的语法 */">待支付订单</div>
		<div  tapmode="" onclick="H.openWin('haigou', '/haigou');">
         <span class="H-icon H-position-relative H-display-inline-block H-float-right H-vertical-middle H-theme-font-color-white H-padding-horizontal-right-10 H-z-index-100"><i class="H-iconfont H-icon-shopping-cart H-font-size-20 H-vertical-middle"></i></span>
        </div>		
    </header> 
    <div id="lis"  class="H-padding-8" style="width:95%;margin-top:44px;">

        <div class="H-padding-10 H-border-vertical-bottom-after  H-theme-background-color-white H-flexbox-horizontal H-theme-font-color-999 H-font-size-14 H-flexbox-horizontal H-text-horizontal-left H-box-sizing-border-box H-margin-vertical-bottom-5" >
           <span class="H-display-block H-flex-item H-theme-font-color-black-active" style="line-height:20px">
          需支付: <span style="color:red;font-size:20px;"><?php echo $temtot ?></span>
           </span>
        </div>
		
	<?php 

  if($is_weixin==2){
 ?>

        <div class="H-padding-vertical-bottom-10"></div>
        <button id="test"    class="H-button H-width-100-percent  H-font-size-12 H-outline-none H-padding-vertical-both-12 H-padding-horizontal-both-20 H-theme-background-color8 H-theme-font-color-white H-theme-border-color8 H-theme-border-color8-click H-theme-background-color8-click H-theme-font-color8-click H-border-radius-3" >支付宝支付</button>
	 <?php } ?>		

	 	<?php 

  if($is_weixin==1){
 ?>

	 
	        <button id="wxgo"    class="H-button H-width-100-percent  H-font-size-12 H-outline-none H-padding-vertical-both-12 H-padding-horizontal-both-20 H-theme-background-color8 H-theme-font-color-white H-theme-border-color8 H-theme-border-color8-click H-theme-background-color8-click H-theme-font-color8-click H-border-radius-3" >微信支付</button>	
		
		
		 <?php } ?>			
		
		
		
    <div class="H-padding-vertical-bottom-90"></div>
  
    </div>


    <script type="text/javascript" src="/ui/script/H.js"></script>
    <script type="text/javascript" src="/ui/js/jquery-1.9.1.min.js"></script>
	
	<?php 

  if($is_weixin==2){
 ?>
	
<script id='spay-script' src='https://jspay.beecloud.cn/1/pay/jsbutton/returnscripts?appId=b6b437fa-c5cf-4e88-801b-c51696582283'></script>
<script>
    //3. 需要发起支付时(示例中绑定在一个按钮的click事件中),调用BC.click方法
    document.getElementById("test").onclick = function() {
        asyncPay();
    };
    function bcPay() {
        BC.click({
			 "amount": <?php echo $amount; ?>,
            "title": "<?php echo $title; ?>",
           
            "out_trade_no": "<?php echo $out_trade_no;?>", //唯一订单号
            "sign" : "<?php echo $sign;?>",
            /**
             * optional 为自定义参数对象，目前只支持基本类型的key ＝》 value, 不支持嵌套对象；
             * 回调时如果有optional则会传递给webhook地址，webhook的使用请查阅文档
             */
            "optional": {"test": "willreturn"},
			"return_url":"http://www.goodshare.com.cn/haigou/dzfdjzfbhd.php"
		
        });
    }
    // 这里不直接调用BC.click的原因是防止用户点击过快，BC的JS还没加载完成就点击了支付按钮。
    // 实际使用过程中，如果用户不可能在页面加载过程中立刻点击支付按钮，就没有必要利用asyncPay的方式，而是可以直接调用BC.click。
    function asyncPay() {
        if (typeof BC == "undefined") {
            if (document.addEventListener) { // 大部分浏览器
                document.addEventListener('beecloud:onready', bcPay, false);
            } else if (document.attachEvent) { // 兼容IE 11之前的版本
                document.attachEvent('beecloud:onready', bcPay);
            }
        } else {
            bcPay();
        }
    }
</script>

   <?php } elseif($is_weixin=1) { ?>	
   
   
   <?php
   $trade_no='b'.rand(100,999);
$data = array(
    "app_id" =>  "92cebded-aa13-4ded-adc3-4870aa68ef90",
    "title" => mb_substr($rowuu['chtitle'],0,15,'utf-8'),
    "amount" => $temtot*100,
    "out_trade_no" => $_COOKIE['dzfhaigoufid'].$trade_no,
    "openid" => $openid,
    "trace_id" => "zsw"
);
$app_secret1 = "320c075b-f5b2-49de-95a3-5b10f6119775";
$sign1 = md5($data['app_id'] . $data['title'] . $data['amount'] . $data['out_trade_no'] . $app_secret1);
$data["sign"] = $sign1;
$data["optional"] = json_decode(json_encode(array("hello" => "1")));
//    $data["openid"] ="o3kKrjlUsMnv__cK5DYZMl0JoAkY";   //o3kKrjlUsMnv__cK5DYZMl0JoAkY   oOCyauJ6nKcXiIIQ_bixiQpaL6PQ(me)
?>
   
   
   
   
	<script id='spay-script' src='https://jspay.beecloud.cn/1/pay/jsbutton/returnscripts?appId=92cebded-aa13-4ded-adc3-4870aa68ef90'></script>
<script>
    document.getElementById("wxgo").onclick = function() {
        asyncPay();
    };

    function bcPay() {
        BC.click(<?php echo json_encode($data) ?>, {
            wxJsapiFinish : function(res) {
                //jsapi接口调用完成后
				 if(res.err_msg == "get_brand_wcpay_request:cancel" ) {					 
				 alert('您取消了支付');	 
					location.reload() ; 	 
				 }
				 if(res.err_msg == "get_brand_wcpay_request:ok" ) {
					 
	              location.href='http://www.goodshare.com.cn/haigou/dzfdjzfbhd.php?bill=<?=$out_trade_no?>&total_fee=<?=$temtot?>';
					 
				 }
               
            }
        });
    }
    
    // 这里不直接调用BC.click的原因是防止用户点击过快，BC的JS还没加载完成就点击了支付按钮。
    // 实际使用过程中，如果用户不可能在页面加载过程中立刻点击支付按钮，就没有必要利用asyncPay的方式，而是可以直接调用BC.click。
    function asyncPay() {
        if (typeof BC == "undefined") {
            if (document.addEventListener) { // 大部分浏览器
                document.addEventListener('beecloud:onready', bcPay, false);
            } else if (document.attachEvent) { // 兼容IE 11之前的版本
                document.attachEvent('beecloud:onready', bcPay);
            }
        } else {
            bcPay();
        }
    }
</script>
 <?php } ?>	
	
	

</body>
</html>